#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof a)
const int MN = 20*20+10;
int k, n, L, tr[MN][64], match[MN], sz, f[MN], vis[MN][100+5], id[256];
double pro[64], d[MN][100+5];
void add(const string &s){
    int p = 0;
    for (char c:s){
        int i = id[c];
        int &nx = tr[p][i];
        if(!nx) ++sz, clr(tr[sz]), nx = sz, match[sz] = 0;
        p = nx;
    }
    match[p] = 1;
}

void getfail(){
    queue<int> q;
    f[0] = 0;
    for (int c = 0; c < n ; ++c) {
        int u = tr[0][c];
        if(u) q.push(u), f[u] = 0;
    }
    while (!q.empty()){
        int u = q.front(); q.pop();
        for (int c = 0; c < n ; ++c) {
            int v = tr[u][c];
            if(!v) {tr[u][c] = tr[f[u]][c]; continue;}
            q.push(v);
            f[v] = tr[f[u]][c];
            match[v] |= match[f[v]];
        }
    }
}

void init(){
    sz = 0;
    clr(tr[0]), clr(vis);
}

double dfs(int u, int l){
    if(l==L) return 1;
    double &ans = d[u][l];
    if(vis[u][l]) return ans;
    vis[u][l] = 1;
    ans = 0;
    for (int c = 0; c < n ; ++c) {
        int nx = tr[u][c];
        if(!match[nx])
            ans += pro[c]*dfs(nx, l+1);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int T; cin >> T;
    for (int t = 1; t <= T ; ++t) {
        init();
        cin >> k;
        string P[20+2];
        for (int i = 0; i < k; ++i) {
            cin >> P[i];
        }
        cin >> n;
        for (int i = 0; i < n; ++i) {
            char c; double p;
            cin >> c >> p;
            id[c] = i;
            pro[i] = p;
        }
        cin >> L;
        for (int i = 0; i < k; ++i) {
            add(P[i]);
        }
        getfail();
        double ans = dfs(0, 0);
        cout << "Case #" << t << ": " << setprecision(6) << fixed << ans << "\n";
    }
    return 0;
}